package com.syqc.monitor.ui.text

import android.view.Gravity
import android.view.View
import android.view.WindowManager
import android.widget.AdapterView
import android.widget.AdapterView.OnItemSelectedListener
import androidx.activity.viewModels
import autodispose2.autoDispose
import com.syqc.choose.car.ChooseCarActivity
import com.syqc.choose.team.ChooseTeamActivity
import com.syqc.choose.teamcar.TeamsActivity
import com.syqc.choose.teamcar.TeamsCarsActivity
import com.syqc.comlib.base.BaseBarActivity
import com.syqc.comlib.rx.doAfterChanged
import com.syqc.comlib.rx.filterFast
import com.syqc.comlib.utils.ActivityResultUtil.launchAct
import com.syqc.comlib.utils.ActivityResultUtil.registerResultOk
import com.syqc.comlib.utils.ToolbarHelper
import com.syqc.monitor.R
import com.syqc.monitor.databinding.ActivityTextSendBinding
import com.syqc.monitor.databinding.IncludeSelectSureBinding
import com.syqc.utils.InputUtil
import com.syqc.utils.Keys
import com.syqc.utils.NetDataUtil

/**
 *@Author Jasper
 *@Time   2021/1/9
 *@Desc   文本信息下发
 */
class TextSendActivity :
    BaseBarActivity<ActivityTextSendBinding>(ActivityTextSendBinding::inflate) {
    private lateinit var mSureVB: IncludeSelectSureBinding
    private val mPhrasePopup by lazy { PhrasePopup(this) }

    private val mViewModel by viewModels<TextSendViewModel>()

    private val carsPopup by lazy {
        TextCarsPopup(this) {
            mViewModel.clear()
        }
    }

    //是否车辆
    private var isCar = true
    private var isIntentFast = false

    override fun initView() {
        mSureVB = IncludeSelectSureBinding.bind(binding.root)
        window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN)
        ToolbarHelper.setToolbar(this, R.string.title_other_message)
        mSureVB.tvSure.setText(R.string.desc_send_cmd)

        InputUtil.setInhibitInputSpaChatRemark(binding.editTextContent, 200)

        mViewModel.tts = "1"
        mViewModel.toastObserver().autoDispose(scopeProvider).subscribe(this::toastObserver)

        mViewModel.sendObserver().autoDispose(scopeProvider).subscribe {
            mSureVB.tvSure.setText(it)
        }

        mViewModel.mNoteLive.observe(this) {
            mPhrasePopup.showAtLocation(window.decorView, it)
        }

        mViewModel.carsLive.observe(this) {
            if (it.isEmpty()) return@observe
            carsPopup.showAtLocation(window.decorView, Gravity.NO_GRAVITY, 0, 0)
            carsPopup.setData(it)
        }

        initIntent()

    }

    private fun initIntent() {
        val carId = intent.getStringExtra(Keys.INTENT_CAR_ID)
        val carName = intent.getStringExtra(Keys.INTENT_PLATE) ?: ""
        if (carId == null) return
        isIntentFast = true
        mViewModel.carId = carId
        binding.acSpinner.isEnabled = false
        binding.tvCarTeam.isEnabled = false
        binding.tvCarTeam.setHint(R.string.hint_toast_choose_car)
        binding.tvCarTeam.text = carName
    }

    override fun setListener() {
        binding.tvTextClear.setOnClickListener {
            mViewModel.textMsg = ""
            binding.editTextContent.setText("")
        }

        binding.cbTextUrgent.setOnClickListener {
            if (binding.cbTextUrgent.isChecked) {
                mViewModel.urgent = "1"
            } else {
                mViewModel.urgent = "0"
            }
        }

        binding.cbTextDisplay.setOnClickListener {
            if (binding.cbTextDisplay.isChecked) {
                mViewModel.display = "1"
            } else {
                mViewModel.display = "0"
            }
        }

        binding.cbTextTTS.setOnClickListener {
            if (binding.cbTextTTS.isChecked) {
                mViewModel.tts = "1"
            } else {
                mViewModel.tts = "0"
            }
        }

        binding.cbTextAdDisplay.setOnClickListener {
            if (binding.cbTextAdDisplay.isChecked) {
                mViewModel.adDisplay = "1"
            } else {
                mViewModel.adDisplay = "0"
            }
        }

        binding.editTextContent.doAfterChanged {

            mViewModel.textMsg = it
        }

        binding.tvTextFastEdit.filterFast {
            editLauncher.launchAct<EditFastDialog>(this)
        }

        binding.tvCarTeam.filterFast {
            mViewModel.carId = ""
            mViewModel.teamId = ""
            binding.tvCarTeam.text = ""
            if (isCar) {
                if (NetDataUtil.isSyncTree) {
                    carLauncher.launchAct<TeamsCarsActivity>(this)
                } else {
                    carLauncher.launchAct<ChooseCarActivity>(this)
                }
            } else {
                if (NetDataUtil.isSyncTree) {
                    teamLauncher.launchAct<TeamsActivity>(this) {
                        putExtra("isMap", true)
                    }
                } else {
                    teamLauncher.launchAct<ChooseTeamActivity>(this) {
                        putExtra("isMap", true)
                    }
                }
            }
        }
        mSureVB.tvSure.filterFast {
            if (isCar) {
                if (mViewModel.isSend) {
                    mViewModel.stopSubscribe()
                } else {
                    mViewModel.sendTextCmdMsg()
                }
            } else {
                mViewModel.clear()
                mViewModel.getCars()
            }

        }

        binding.tvTextNormal.filterFast {
            mViewModel.cmdNote()
        }

        mPhrasePopup.setOnCallback(object : PhrasePopup.OnCallback {
            override fun callback(desc: String) {
                binding.editTextContent.setText(desc)
                binding.editTextContent.setSelection(binding.editTextContent.text.length)
            }
        })


        binding.acSpinner.onItemSelectedListener = object : OnItemSelectedListener {
            override fun onItemSelected(
                parent: AdapterView<*>?, view: View?, position: Int, id: Long
            ) {
                if (isIntentFast) return
                binding.tvCarTeam.text = ""
                mViewModel.carId = ""
                mViewModel.teamId = ""
                isCar = position == 0
                if (isCar) {
                    binding.tvCarTeam.setHint(R.string.hint_toast_choose_car)
                } else {
                    binding.tvCarTeam.setHint(R.string.hint_toast_choose_team)
                }

            }

            override fun onNothingSelected(parent: AdapterView<*>?) {

            }
        }
    }

    private val carLauncher = registerResultOk { result ->
        result.data?.let {
            val carId = it.getStringExtra(Keys.INTENT_CAR_ID) ?: ""
            val carName = it.getStringExtra(Keys.INTENT_CAR_NAME) ?: ""
            mViewModel.carId = carId
            binding.tvCarTeam.text = carName
        }
    }
    private val editLauncher = registerResultOk { result ->
        binding.editTextContent.setText(result.data?.getStringExtra(Keys.INTENT_OTHER) ?: "")
    }

    private val teamLauncher = registerResultOk { result ->
        result.data?.let {
            val teamId = it.getStringExtra(Keys.INTENT_TEAM_ID) ?: ""
            val teamName = it.getStringExtra(Keys.INTENT_TEAM_NAME) ?: ""
            mViewModel.teamId = teamId
            binding.tvCarTeam.text = teamName
        }
    }

}